import {defineStore} from "pinia";
import {computed, ref} from "vue";
import {StorageSerializers, useStorage} from "@vueuse/core";

export const useLoginStore = defineStore("loginStore", () => {
    const state = ref(null)

    useStorage("login-info",state,undefined,{serializer:StorageSerializers.object})
    const set = (v) => state.value = v
    const clear = () => state.value = null

    const isLogin = computed(() => !!state.value)

    const isAdmin = computed(() => () => state.value?.admin)

    const token = computed(() => state.value?.access_token)

    const isExpired = computed(() => new Date() >= new Date(state.value?.expires_time))

    return {set, clear,isLogin,token,isExpired,isAdmin}
})